/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package merlion.scm.rmpm.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import merlion.common.util.Consts;

/**
 *
 * @author Kenn
 */
@Entity
public class GoodsReceiptRating implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private Double overall;
    @OrderBy("index")
    private List<CriterionRating> ratings = new ArrayList<CriterionRating>();

    public GoodsReceiptRating() {

        CriterionRating c1 = new CriterionRating(0, "Quality");
        CriterionRating c2 = new CriterionRating(1, "Price");
        CriterionRating c3 = new CriterionRating(2, "Lead Time");
        CriterionRating c4 = new CriterionRating(3, "Service");
        
        ratings.add(c1);
        ratings.add(c2);
        ratings.add(c3);
        ratings.add(c4);
    }

    public Double calculateOverallRating() {

        Long quality = ratings.get(0).getRating();
        Long price = ratings.get(1).getRating();
        Long leadtime = ratings.get(2).getRating();
        Long service = ratings.get(3).getRating();
        
        Long qualityWeightage = Consts.QUALITY_WEIGHTAGE;
        Long priceWeightage = Consts.PRICE_WEIGHTAGE;
        Long leadtimeWeightage = Consts.LEADTIME_WEIGHTAGE;
        Long serviceWeightage = Consts.SERVICE_WEIGHTAGE;

        double cal = ((((double) quality * (double) qualityWeightage) + ((double) price * (double) priceWeightage) + ((double) leadtime * (double) leadtimeWeightage) + ((double) service * (double) serviceWeightage)) / ((double) qualityWeightage + (double) priceWeightage + (double) leadtimeWeightage + (double) serviceWeightage));
        setOverall(cal);

        return cal;

    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Double getOverall() {
        return overall;
    }

    public void setOverall(Double overall) {
        this.overall = overall;
    }

    public List<CriterionRating> getRatings() {
        return ratings;
    }

    public void setRatings(List<CriterionRating> ratings) {
        this.ratings = ratings;
    }
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof GoodsReceiptRating)) {
            return false;
        }
        GoodsReceiptRating other = (GoodsReceiptRating) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "merlion.scm.rmpm.entity.SupplierRating[ id=" + id + " ]";
    }
}
